/**
 * CoffeeMilk (C) 2010 Amen Hamdan
 * 
 * See the NOTICE file distributed with this work for additional 
 * information regarding copyright ownership.  This file is licenses
 * to you under the Apache License, Version 2.0 (the "License"); you 
 * may not use this file except in compliance with the License.  You 
 * may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 *     
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 
 * implied. See the License for the specific language governing 
 * permissions and limitations under the License.    
 * 
 */
package ameen.rtm.coffeemilk.ui.cellrenderer;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Vector;

import javax.swing.ImageIcon;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;

import ameen.rtm.lib.types.RtmTask;

@SuppressWarnings("serial")
public class TaskTableCellRenderer extends DefaultTableCellRenderer {
	private Vector<RtmTask> fTaskVector;
	private SimpleDateFormat fDateFormatter = new SimpleDateFormat("d MMM yyyy");
	private ImageIcon fEditIcon = new ImageIcon(getClass().getResource("edit.png"));
	private ImageIcon fNotesIcon = new ImageIcon(getClass().getResource("notes.png"));

	public TaskTableCellRenderer(Vector<RtmTask> taskVector) {
		fTaskVector = taskVector;
	}
	
	public Component getTableCellRendererComponent(JTable table, Object value, 
			boolean isSelected, boolean hasFocus, int row, int column) {  

		JLabel label = (JLabel) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);  
		final RtmTask currentTask = fTaskVector.get(row);
		
		switch (column) {
		case 0:
			updatePriorityLable(label, currentTask);
			break;

		case 1:
			final JCheckBox checkBox  = new JCheckBox();
			checkBox.setSelected(currentTask.isCompleted());
			return checkBox;
			
		case 2:
			if(currentTask.isCompleted()) {
				label.setForeground(Color.LIGHT_GRAY);
			} else {
				label.setForeground(Color.BLACK);
			}
			String nameToolTip = createToolTipText(currentTask);
			label.setToolTipText(nameToolTip);

			break;
			
		case 3:
			Date date = (Date) value;
			if(date == null) {
				label.setText("no due");
				label.setForeground(Color.lightGray);
			} else {
				label.setText(fDateFormatter.format(date));
				if(date.before(new Date())) {
					label.setForeground(new Color(0xAA3030));
				} else {
					label.setForeground(Color.BLACK);
				}
			}
			label.setFont(new Font("Dialog", Font.PLAIN, 12));
			label.setHorizontalAlignment(SwingConstants.RIGHT);
			
			String dueDateToolTip = createToolTipText(currentTask);
			label.setToolTipText(dueDateToolTip);
			break;
			
		case 4:
			label.setHorizontalAlignment(SwingConstants.CENTER);
			label.setIcon(fEditIcon);
			break;	
			
		case 5:
			label.setHorizontalAlignment(SwingConstants.CENTER);
			label.setIcon(fNotesIcon);
			break;
		}
		return label;
	}

	private String createToolTipText(RtmTask currentTask) {
		StringBuffer toolTip = new StringBuffer();
		toolTip.append("<HTML>");
		toolTip.append("Tags: ");
		if( (currentTask.getTags() != null) && (currentTask.getTags().size() > 0)) {
			toolTip.append(currentTask.getTagsString());
		} else {
			toolTip.append(" [ no tags ]");
		}
		toolTip.append("</HTML>");
		return toolTip.toString();
	}  
	
	private void updatePriorityLable(JLabel label, RtmTask task) {
		label.setText(" ");
		Color color = Color.LIGHT_GRAY;
		switch (task.getPriority()) {
		case 1:
			color = new Color(0xEA5200);
			break;

		case 2:
			color = new Color(0x0060BF);
			break;
		
		case 3:
			color = new Color(0x359AFF);
			break;
		}
	
		label.setBackground(color);
	}
}
